package com.chaos.system.service.client;

import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.result.data.BooleanResult;
import com.chaos.framework.model.dto.result.data.DataResult;
import com.chaos.framework.model.dto.result.list.ListResult;
import com.chaos.framework.starter.base.BaseController;
import com.chaos.system.api.data.TUserData;
import com.chaos.system.api.data.UserSessionKeyData;
import com.chaos.system.api.feign.UserFeign;
import com.chaos.system.service.TUserService;
import com.chaos.system.service.UserSessionKeyService;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@Hidden
public class UserClient extends BaseController implements UserFeign {

    private final TUserService tUserService;
    private final UserSessionKeyService userSessionKeyService;

    @Override
    public ListResult<TUserData> list(TUserData queryData) {
        return listResult(tUserService.selectByData(queryData));
    }

    @Override
    public DataResult<TUserData> selectByWxOpenId(String openId, Long tenantId) {
        return dataResult(tUserService.selectByWxOpenId(openId, tenantId));
    }

    @Override
    public DataResult<ID> insertModel(TUserData user) {
        return dataResult(tUserService.insertModel(user));

    }

    @Override
    public BooleanResult updateUserInfoId(Long userInfoId) {
        Long userId = getUserIdNotNull();
        TUserData tUserData = new TUserData();
        tUserData.setId(userId);
        tUserData.setUserInfoId(userInfoId);
        boolean result = tUserService.updateModelByID(tUserData);
        return booleanResult(result);
    }

    @Override
    public DataResult<ID> insertModel(UserSessionKeyData data) {
        ID id = userSessionKeyService.insertModel(data);
        return dataResult(id);

    }

    @Override
    public DataResult<String> selectLastUserSessionKey() {
        Long userId = getUserIdNotNull();
        return dataResult(userSessionKeyService.selectLatestSessionKeyByUserId(userId));
    }

    @Override
    public DataResult<TUserData> register(TUserData userData) {
        return dataResult((ID) null);
    }


}

